package com.white.controller;

import com.white.bean.SearchHistory;
import com.white.service.SearchHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 搜索历史控制器：
 * 1.添加搜索历史
 * 2.删除搜索历史
 * 3.查询搜索历史
 */
@Controller
@RequestMapping("/searchHistory")
public class SearchHistoryController {
    @Autowired
    SearchHistoryService searchHistoryService;

    /**
     * 新增搜索历史
     * @param userId 用户id
     * @param content 搜索内容
     */
    @RequestMapping("/create/{content}")
    @ResponseBody
    public boolean create(@CookieValue(UserController.LOGIN_USER_ID) Long userId,
                       @PathVariable("content") String content) {
        return searchHistoryService.create(new SearchHistory(userId, content));
    }

    /**
     * 通过id删除搜索历史
     * @param searchHistoryId 搜索历史id
     * @return
     */
    @RequestMapping("/delete/{searchHistoryId}")
    @ResponseBody
    public void delete(@PathVariable("searchHistoryId") Long searchHistoryId) {
        searchHistoryService.deleteById(searchHistoryId);
    }

    /**
     * 通过用户id获取全部搜索历史
     * @param userId 用户id
     * @return
     */
    @RequestMapping("/searchByUserId")
    @ResponseBody
    public List<SearchHistory> searchByUserId(@CookieValue(UserController.LOGIN_USER_ID) Long userId) {
        return searchHistoryService.searchByUserId(userId);
    }

}
